<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Using the has_template_(xxx) metafunction</title>
<link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../index.html" title="Chapter 1. The Type Traits Introspection Library">
<link rel="up" href="../tti_detail_has_template.html" title="Introspecting an inner class template">
<link rel="prev" href="../tti_detail_has_template.html" title="Introspecting an inner class template">
<link rel="next" href="../tti_function_templates.html" title="Introspecting function templates technique">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../tti_detail_has_template.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tti_detail_has_template.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../tti_function_templates.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="the_type_traits_introspection_library.tti_detail_has_template.tti_detail_has_template_metafunction"></a><a class="link" href="tti_detail_has_template_metafunction.html" title="Using the has_template_(xxx) metafunction">Using
      the has_template_(xxx) metafunction</a>
</h3></div></div></div>
<h5>
<a name="the_type_traits_introspection_library.tti_detail_has_template.tti_detail_has_template_metafunction.h0"></a>
        <span class="phrase"><a name="the_type_traits_introspection_library.tti_detail_has_template.tti_detail_has_template_metafunction.generating_the_metafunction"></a></span><a class="link" href="tti_detail_has_template_metafunction.html#the_type_traits_introspection_library.tti_detail_has_template.tti_detail_has_template_metafunction.generating_the_metafunction">Generating
        the metafunction</a>
      </h5>
<p>
        You generate the metafunction by invoking the macro with the name of an inner
        class template:
      </p>
<pre class="programlisting"><span class="comment">// `template type parameters` form</span>

<span class="identifier">BOOST_TTI_HAS_TEMPLATE</span><span class="special">(</span><span class="identifier">AClassTemplate</span><span class="special">,</span><span class="identifier">BOOST_PP_NIL</span><span class="special">)</span> <span class="comment">// non-variadic macro</span>
<span class="identifier">BOOST_TTI_HAS_TEMPLATE</span><span class="special">(</span><span class="identifier">AClassTemplate</span><span class="special">)</span>              <span class="comment">// variadic macro</span>

<span class="comment">// `specific parameters` form</span>

<span class="identifier">BOOST_TTI_HAS_TEMPLATE</span><span class="special">(</span><span class="identifier">AClassTemplate</span><span class="special">,(</span><span class="number">2</span><span class="special">,(</span><span class="keyword">class</span><span class="special">,</span><span class="keyword">int</span><span class="special">)))</span> <span class="comment">// non-variadic macro</span>
<span class="identifier">BOOST_TTI_HAS_TEMPLATE</span><span class="special">(</span><span class="identifier">AClassTemplate</span><span class="special">,</span><span class="keyword">class</span><span class="special">,</span><span class="keyword">int</span><span class="special">)</span>       <span class="comment">// variadic macro</span>
</pre>
<p>
        generates a metafunction called 'has_template_AClassTemplate' in the current
        scope.
      </p>
<p>
        If you want to introspect the same class template name using both the <code class="computeroutput"><span class="keyword">template</span> <span class="identifier">type</span>
        <span class="identifier">parameters</span></code> form and the <code class="computeroutput"><span class="identifier">specific</span> <span class="identifier">parameters</span></code>
        form you will have the problem that you will be generating a metafunction
        of the same name and violating the C++ ODR rule. In this particular case
        you can use the alternate BOOST_TTI_TRAIT_HAS_TEMPLATE macro to name the
        particular metafunction which will be generated.
      </p>
<h5>
<a name="the_type_traits_introspection_library.tti_detail_has_template.tti_detail_has_template_metafunction.h1"></a>
        <span class="phrase"><a name="the_type_traits_introspection_library.tti_detail_has_template.tti_detail_has_template_metafunction.invoking_the_metafunction"></a></span><a class="link" href="tti_detail_has_template_metafunction.html#the_type_traits_introspection_library.tti_detail_has_template.tti_detail_has_template_metafunction.invoking_the_metafunction">Invoking
        the metafunction</a>
      </h5>
<p>
        You invoke the metafunction by instantiating the template with an enclosing
        type to introspect. A return value called 'value' is a compile time bool
        constant.
      </p>
<pre class="programlisting"><span class="identifier">has_template_AType</span><span class="special">&lt;</span><span class="identifier">Enclosing_Type</span><span class="special">&gt;::</span><span class="identifier">value</span>
</pre>
<h5>
<a name="the_type_traits_introspection_library.tti_detail_has_template.tti_detail_has_template_metafunction.h2"></a>
        <span class="phrase"><a name="the_type_traits_introspection_library.tti_detail_has_template.tti_detail_has_template_metafunction.examples"></a></span><a class="link" href="tti_detail_has_template_metafunction.html#the_type_traits_introspection_library.tti_detail_has_template.tti_detail_has_template_metafunction.examples">Examples</a>
      </h5>
<p>
        First we generate metafunctions for various inner class template names:
      </p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">tti</span><span class="special">/</span><span class="identifier">has_template</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="comment">// Using variadic macro, `template type parameters`</span>

<span class="identifier">BOOST_TTI_HAS_TEMPLATE</span><span class="special">(</span><span class="identifier">Template1</span><span class="special">)</span>
<span class="identifier">BOOST_TTI_HAS_TEMPLATE</span><span class="special">(</span><span class="identifier">Template2</span><span class="special">)</span>
<span class="identifier">BOOST_TTI_HAS_TEMPLATE</span><span class="special">(</span><span class="identifier">Template3</span><span class="special">)</span>
<span class="identifier">BOOST_TTI_HAS_TEMPLATE</span><span class="special">(</span><span class="identifier">Template4</span><span class="special">)</span>
<span class="identifier">BOOST_TTI_HAS_TEMPLATE</span><span class="special">(</span><span class="identifier">Template5</span><span class="special">)</span>

<span class="comment">// or using non-variadic macro, `template type parameters`</span>

<span class="identifier">BOOST_TTI_HAS_TEMPLATE</span><span class="special">(</span><span class="identifier">Template1</span><span class="special">,</span><span class="identifier">BOOST_PP_NIL</span><span class="special">)</span>
<span class="identifier">BOOST_TTI_HAS_TEMPLATE</span><span class="special">(</span><span class="identifier">Template2</span><span class="special">,</span><span class="identifier">BOOST_PP_NIL</span><span class="special">)</span>
<span class="identifier">BOOST_TTI_HAS_TEMPLATE</span><span class="special">(</span><span class="identifier">Template3</span><span class="special">,</span><span class="identifier">BOOST_PP_NIL</span><span class="special">)</span>
<span class="identifier">BOOST_TTI_HAS_TEMPLATE</span><span class="special">(</span><span class="identifier">Template4</span><span class="special">,</span><span class="identifier">BOOST_PP_NIL</span><span class="special">)</span>
<span class="identifier">BOOST_TTI_HAS_TEMPLATE</span><span class="special">(</span><span class="identifier">Template5</span><span class="special">,</span><span class="identifier">BOOST_PP_NIL</span><span class="special">)</span>

<span class="comment">// Using variadic macro, `specific parameters`</span>

<span class="identifier">BOOST_TTI_HAS_TEMPLATE</span><span class="special">(</span><span class="identifier">Template6</span><span class="special">,</span><span class="keyword">class</span><span class="special">,</span><span class="keyword">int</span><span class="special">)</span>
<span class="identifier">BOOST_TTI_HAS_TEMPLATE</span><span class="special">(</span><span class="identifier">Template7</span><span class="special">,</span><span class="keyword">typename</span><span class="special">,</span><span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span><span class="special">,</span><span class="keyword">class</span><span class="special">&gt;</span> <span class="keyword">struct</span><span class="special">,</span><span class="keyword">long</span><span class="special">)</span>
<span class="identifier">BOOST_TTI_HAS_TEMPLATE</span><span class="special">(</span><span class="identifier">Template8</span><span class="special">,</span><span class="keyword">double</span><span class="special">,</span><span class="keyword">typename</span><span class="special">)</span>
<span class="identifier">BOOST_TTI_HAS_TEMPLATE</span><span class="special">(</span><span class="identifier">Template9</span><span class="special">,</span><span class="keyword">typename</span><span class="special">,</span><span class="keyword">class</span><span class="special">,</span><span class="keyword">typename</span><span class="special">,</span><span class="keyword">class</span><span class="special">,</span><span class="keyword">typename</span><span class="special">,</span><span class="keyword">short</span><span class="special">)</span>

<span class="comment">// or using non-variadic macro, `specific parameters`</span>

<span class="identifier">BOOST_TTI_HAS_TEMPLATE</span><span class="special">(</span><span class="identifier">Template6</span><span class="special">,(</span><span class="number">2</span><span class="special">,(</span><span class="keyword">class</span><span class="special">,</span><span class="keyword">int</span><span class="special">)))</span>
<span class="identifier">BOOST_TTI_HAS_TEMPLATE</span><span class="special">(</span><span class="identifier">Template7</span><span class="special">,(</span><span class="number">4</span><span class="special">,(</span><span class="keyword">typename</span><span class="special">,</span><span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span><span class="special">,</span><span class="keyword">class</span><span class="special">&gt;</span> <span class="keyword">struct</span><span class="special">,</span><span class="keyword">long</span><span class="special">)))</span>
<span class="identifier">BOOST_TTI_HAS_TEMPLATE</span><span class="special">(</span><span class="identifier">Template8</span><span class="special">,(</span><span class="number">2</span><span class="special">,(</span><span class="keyword">double</span><span class="special">,</span><span class="keyword">typename</span><span class="special">)))</span>
<span class="identifier">BOOST_TTI_HAS_TEMPLATE</span><span class="special">(</span><span class="identifier">Template9</span><span class="special">,(</span><span class="number">6</span><span class="special">,(</span><span class="keyword">typename</span><span class="special">,</span><span class="keyword">class</span><span class="special">,</span><span class="keyword">typename</span><span class="special">,</span><span class="keyword">class</span><span class="special">,</span><span class="keyword">typename</span><span class="special">,</span><span class="keyword">short</span><span class="special">)))</span>
</pre>
<p>
        Next let us create some user-defined types we want to introspect.
      </p>
<pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">Top</span>
  <span class="special">{</span>
  <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">X</span><span class="special">&gt;</span> <span class="keyword">struct</span> <span class="identifier">Template1</span> <span class="special">{</span> <span class="special">};</span>
  <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">A</span><span class="special">,</span><span class="keyword">typename</span> <span class="identifier">B</span><span class="special">,</span><span class="keyword">typename</span> <span class="identifier">C</span><span class="special">&gt;</span> <span class="keyword">class</span> <span class="identifier">Template2</span> <span class="special">{</span> <span class="special">};</span>
  <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">A</span><span class="special">,</span><span class="keyword">typename</span> <span class="identifier">B</span><span class="special">,</span><span class="keyword">typename</span> <span class="identifier">C</span><span class="special">,</span><span class="keyword">int</span> <span class="identifier">D</span><span class="special">&gt;</span> <span class="keyword">class</span> <span class="identifier">Template3</span> <span class="special">{</span> <span class="special">};</span>
  <span class="special">};</span>
<span class="keyword">struct</span> <span class="identifier">Top2</span>
  <span class="special">{</span>
  <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">A</span><span class="special">,</span><span class="keyword">typename</span> <span class="identifier">B</span><span class="special">,</span><span class="keyword">typename</span> <span class="identifier">C</span><span class="special">,</span><span class="keyword">class</span> <span class="identifier">D</span><span class="special">&gt;</span> <span class="keyword">class</span> <span class="identifier">Template3</span> <span class="special">{</span> <span class="special">};</span>
  <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">X</span><span class="special">,</span><span class="keyword">typename</span> <span class="identifier">Y</span><span class="special">&gt;</span> <span class="keyword">struct</span> <span class="identifier">Template4</span> <span class="special">{</span> <span class="special">};</span>
  <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">A</span><span class="special">,</span><span class="keyword">class</span> <span class="identifier">B</span><span class="special">,</span><span class="keyword">typename</span> <span class="identifier">C</span><span class="special">,</span><span class="keyword">class</span> <span class="identifier">D</span><span class="special">,</span><span class="keyword">typename</span> <span class="identifier">E</span><span class="special">&gt;</span> <span class="keyword">class</span> <span class="identifier">Template5</span> <span class="special">{</span> <span class="special">};</span>
  <span class="special">};</span>
<span class="keyword">struct</span> <span class="identifier">Top3</span>
  <span class="special">{</span>
  <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">X</span><span class="special">,</span><span class="keyword">int</span> <span class="identifier">Y</span><span class="special">&gt;</span> <span class="keyword">struct</span> <span class="identifier">Template6</span> <span class="special">{</span> <span class="special">};</span>
  <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">A</span><span class="special">,</span><span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span><span class="special">,</span><span class="keyword">class</span><span class="special">&gt;</span> <span class="keyword">struct</span> <span class="identifier">B</span><span class="special">,</span><span class="keyword">long</span> <span class="identifier">C</span><span class="special">&gt;</span> <span class="keyword">class</span> <span class="identifier">Template7</span> <span class="special">{</span> <span class="special">};</span>
  <span class="special">};</span>
<span class="keyword">struct</span> <span class="identifier">Top4</span>
  <span class="special">{</span>
  <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">double</span> <span class="identifier">X</span><span class="special">,</span><span class="keyword">typename</span> <span class="identifier">Y</span><span class="special">&gt;</span> <span class="keyword">struct</span> <span class="identifier">Template8</span> <span class="special">{</span> <span class="special">};</span>
  <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">A</span><span class="special">,</span><span class="keyword">class</span> <span class="identifier">B</span><span class="special">,</span><span class="keyword">typename</span> <span class="identifier">C</span><span class="special">,</span><span class="keyword">class</span> <span class="identifier">D</span><span class="special">,</span><span class="keyword">typename</span> <span class="identifier">E</span><span class="special">,</span><span class="keyword">short</span> <span class="identifier">F</span><span class="special">&gt;</span> <span class="keyword">class</span> <span class="identifier">Template9</span> <span class="special">{</span> <span class="special">};</span>
  <span class="special">};</span>
</pre>
<p>
        Finally we invoke our metafunction and return our value. This all happens
        at compile time, and can be used by programmers doing compile time template
        metaprogramming.
      </p>
<pre class="programlisting"><span class="identifier">has_template_Template1</span><span class="special">&lt;</span><span class="identifier">Top</span><span class="special">&gt;::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// true</span>
<span class="identifier">has_template_Template1</span><span class="special">&lt;</span><span class="identifier">Top2</span><span class="special">&gt;::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// false</span>

<span class="identifier">has_template_Template2</span><span class="special">&lt;</span><span class="identifier">Top</span><span class="special">&gt;::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// true</span>
<span class="identifier">has_template_Template2</span><span class="special">&lt;</span><span class="identifier">Top2</span><span class="special">&gt;::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// false</span>

<span class="identifier">has_template_Template3</span><span class="special">&lt;</span><span class="identifier">Top</span><span class="special">&gt;::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// false, not all typename/class template parameters</span>
<span class="identifier">has_template_Template3</span><span class="special">&lt;</span><span class="identifier">Top2</span><span class="special">&gt;::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// true</span>

<span class="identifier">has_template_Template4</span><span class="special">&lt;</span><span class="identifier">Top</span><span class="special">&gt;::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// false</span>
<span class="identifier">has_template_Template4</span><span class="special">&lt;</span><span class="identifier">Top2</span><span class="special">&gt;::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// true</span>

<span class="identifier">has_template_Template5</span><span class="special">&lt;</span><span class="identifier">Top</span><span class="special">&gt;::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// false</span>
<span class="identifier">has_template_Template5</span><span class="special">&lt;</span><span class="identifier">Top2</span><span class="special">&gt;::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// true</span>

<span class="identifier">has_template_Template6</span><span class="special">&lt;</span><span class="identifier">Top3</span><span class="special">&gt;::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// true</span>
<span class="identifier">has_template_Template6</span><span class="special">&lt;</span><span class="identifier">Top4</span><span class="special">&gt;::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// false</span>

<span class="identifier">has_template_Template7</span><span class="special">&lt;</span><span class="identifier">Top3</span><span class="special">&gt;::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// true</span>
<span class="identifier">has_template_Template7</span><span class="special">&lt;</span><span class="identifier">Top4</span><span class="special">&gt;::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// false</span>

<span class="identifier">has_template_Template8</span><span class="special">&lt;</span><span class="identifier">Top3</span><span class="special">&gt;::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// false</span>
<span class="identifier">has_template_Template8</span><span class="special">&lt;</span><span class="identifier">Top4</span><span class="special">&gt;::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// true</span>

<span class="identifier">has_template_Template9</span><span class="special">&lt;</span><span class="identifier">Top3</span><span class="special">&gt;::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// false</span>
<span class="identifier">has_template_Template9</span><span class="special">&lt;</span><span class="identifier">Top4</span><span class="special">&gt;::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// true</span>
</pre>
<h5>
<a name="the_type_traits_introspection_library.tti_detail_has_template.tti_detail_has_template_metafunction.h3"></a>
        <span class="phrase"><a name="the_type_traits_introspection_library.tti_detail_has_template.tti_detail_has_template_metafunction.metafunction_re_use"></a></span><a class="link" href="tti_detail_has_template_metafunction.html#the_type_traits_introspection_library.tti_detail_has_template.tti_detail_has_template_metafunction.metafunction_re_use">Metafunction
        re-use</a>
      </h5>
<p>
        The macro encodes the name of the inner class template for which we are searching,
        the fact that we are introspecting for a class template within an enclosing
        type, and optionally the template parameters for that class template.
      </p>
<p>
        Once we create our metafunction for introspecting an inner class template
        by name, we can reuse the metafunction for introspecting any enclosing type,
        having any inner class template, for that name.
      </p>
<p>
        However we need to understand that we are restricted in our reuse of the
        metafunction by whether we originally use the template type parameters form
        or the specific form. In either case we are always introspecting an inner
        class template which matches that form. In the case of the template type
        parameters form, any inner class template for which we are introspecting
        must have all template type parameters, as well as the correct name. In the
        case of the specific parameters form, any inner class template for which
        we are introspecting must have template parameters which match the specific
        template parameters passed to the macro, as well as the correct name.
      </p>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2011-2013 Tropic Software
      East Inc<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../tti_detail_has_template.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tti_detail_has_template.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../tti_function_templates.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
